package cn.sirun.com.fca.personal.center.loader;

import android.support.annotation.NonNull;

import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.recycler.flow.BaseRecyclerLoader;
import com.tencent.cloud.iov.recycler.item.IBlockItem;

import java.util.ArrayList;
import java.util.List;

import cn.sirun.com.fca.account.model.base.CarInfoDetailResponse;
import cn.sirun.com.fca.account.model.login.PersonalInfo;
import cn.sirun.com.fca.account.model.login.PersonalInfoRequest;
import cn.sirun.com.fca.personal.center.block.item.impl.CarInfoItem;
import cn.sirun.com.fca.personal.center.block.item.impl.OperationItenm;
import cn.sirun.com.fca.personal.center.block.item.impl.PersonalInfoItem;
import cn.sirun.com.fca.personal.center.block.item.impl.TrafficItem;
import cn.sirun.com.fca.personal.center.model.response.CityResponse;
import cn.sirun.com.fca.personal.center.model.service.PersonalInfoManager;
import cn.sirun.com.fca.personal.center.repo.ProfileRepository;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;
import io.reactivex.functions.Function3;
import io.reactivex.schedulers.Schedulers;

public class PersonalCenterLoader extends PersonalCenterFlowLoader {


    public void refreshData() {
        PersonalInfoManager.getInstance().refreshPersonalInfoIfNeeded();
    }

    public Observable<BaseResponse<PersonalInfo>> getPersonalInfo(PersonalInfoRequest request) {
        return ProfileRepository.getInstance().getPersonalInfo(request)
                .subscribeOn(Schedulers.io());
    }

    public Observable<BaseResponse<CarInfoDetailResponse>> getCarInfo(String din) {
        return ProfileRepository.getInstance().getCarInfoDetail(din)
                .subscribeOn(Schedulers.io());
    }
//    @NonNull
//    @Override
//    protected Observable<List<IBlockItem>> getDataObservable() {
//        Observable<List<IBlockItem>> personalInfoObservable = getPersonalInfoObservable();
//
//
//        List<IBlockItem> carInfolist = new ArrayList<>();
//        carInfolist.add(new CarInfoItem("", "Alfaromeo", "VIN：LSGXE8355HD12345"));
//        Observable<List<IBlockItem>> carInfoObservable =Observable.just(carInfolist);
//
//        List<IBlockItem> list = new ArrayList<>();
//        list.add(new TrafficItem());
//        list.add(new OperationItenm());
//        Observable<List<IBlockItem>> otherObservable =Observable.just(list);
//
//        return Observable.combineLatest(
//                personalInfoObservable,carInfoObservable,otherObservable,
//                new Function3<List<IBlockItem>, List<IBlockItem>, List<IBlockItem>,
//                        List<IBlockItem>>() {
//                    @Override
//                    public List<IBlockItem> apply(List<IBlockItem> personalInfoItems,
//                                                  List<IBlockItem> carInfoItems,
//                                                  List<IBlockItem> operationListItems) throws
//                            Exception {
//                        List<IBlockItem> list = new ArrayList<>();
//
//                        addItems(list, personalInfoItems);
//                        addItems(list, carInfoItems);
//                        addItems(list, operationListItems);
//                        return list;
//                    }
//                })
//                .subscribeOn(Schedulers.io());
//
//    }
//
//
//    /**
//     * 个人信息
//     *
//     * @return
//     */
//    private Observable<List<IBlockItem>> getPersonalInfoObservable() {
//        return PersonalInfoManager.getInstance().getPersonalInfoObservable()
//                .startWith(new PersonalInfo())
//                .flatMap(new Function<PersonalInfo, ObservableSource<List<IBlockItem>>>() {
//                    @Override
//                    public ObservableSource<List<IBlockItem>> apply(final PersonalInfo
//                                                                            personalInfo) throws
//                            Exception {
//                        if (personalInfo == null) {
//                            return Observable.empty();
//                        }
//
//                        List<IBlockItem> items = new ArrayList<IBlockItem>() {{
//                            add(new PersonalInfoItem(personalInfo));
//                        }};
//                        return Observable.just(items);
//                    }
//                })
//                .distinctUntilChanged();
//    }
//
//    private void addItems(@NonNull List<IBlockItem> list, List<IBlockItem> items) {
//        if (items != null && !items.isEmpty()) {
//            list.addAll(items);
//        }
//    }
}
